<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once 'database.php';

class chat_handler extends database {

    public $result;
    public $nama;
    public $img_url;
    public $username;

    function __construct() {
        parent::__construct();
    }

    function __destruct() {

    }

    function create_group($arr_user){
         $q = "SELECT max(group_id) AS maks FROM group_list";
            $this->exec_q($q, "maks");
            $id = $this->result[0];
            $id++;
            $g_name = "Group " . $id;

            //buat group
            $q_group = "INSERT INTO group_list (group_name,chat) VALUES ('$g_name',1)";
            $this->connect();
            mysql_query($q_group, $this->conn);
            mysql_close();

            //masukin anggotanya
            for ($i = 0; $i < count($arr_user); $i++) {
                $user = $arr_user[$i];

                $q_reg = "INSERT INTO group_members VALUES ($id,'$user')";
                $this->connect();
                mysql_query($q_reg, $this->conn);
            }
            mysql_close();

            return $id;
    }

    function send_message($g_id, $author, $body) {
        $q = "INSERT INTO chat (group_id,chat_body,author) VALUES ($g_id,'$body','$author')";

        $this->connect();
        mysql_query($q, $this->conn);
        mysql_close();
    }

    function retr_message_chat($group_id/*, $chat_id*/) {

        $q = "SELECT * FROM chat WHERE group_id=$group_id ORDER BY chat_id DESC";
        $this->exec_q($q, "author");
        $username = $this->result;

        for($i=0; $i<count($username); $i++){
            if ($username[$i] != null){
                $usr = $username[$i];
                $query = "SELECT nama_lengkap FROM user WHERE usr_email='$usr'";
                $this->exec_q($query, "nama_lengkap");
                $res2 = $this->result[0];
                $res[0][$i] = $res2;

            }
        }

        $this->exec_q($q, "chat_body");
        $res[1] = $this->result;

        $this->result = $res;
    }
    
    function get_user_online($u){
       //return nama lengkap, profpic url


        //yang follower
        $q_follower = "SELECT online.usr_email,online.profpic_id FROM
                        usr_followers,(SELECT * FROM user WHERE online=1 AND usr_email!='$u') as online
                        WHERE usr_followers.usr_email='$u' AND online.usr_email=usr_followers.usr_follower";
        
        $this->exec_q($q_follower, "online.usr_email");
        $username = $this->result;
        
        $this->username[0] = $username;

        for ($i=0; $i<count($username); $i++){
            if ($username[$i] != null){
                $usr = $username[$i];
                $query = "SELECT nama_lengkap FROM user WHERE usr_email='$usr'";
                $this->exec_q($query, "nama_lengkap");
                $res = $this->result[0];
                $this->nama[0][$i] = $res;

            }
        }
        
        $this->exec_q($q_follower, "profpic_id");
        $profpic_id = $this->result;

        //cari URLnya

        for ($i=0; $i<count($profpic_id); $i++){
            if ($profpic_id[$i] != null){
                $id = $profpic_id[$i];
                $query = "SELECT url FROM photo WHERE photo_id=$id";
                $this->exec_q($query, "url");
                $res = $this->result[0];
                $this->img_url[0][$i] = $res;
            }
        }

         //yang following
        $q_following = "SELECT online.usr_email,online.profpic_id FROM
                        usr_followers,(SELECT * FROM user WHERE online=1 AND usr_email!='$u') as online
                        WHERE usr_followers.usr_follower='$u' AND online.usr_email=usr_followers.usr_email";

        $this->exec_q($q_following, "online.usr_email");
        $username = $this->result;

        $this->username[1] = $username;

        for ($i=0; $i<count($username); $i++){
            if ($username[$i] != null){
                $usr = $username[$i];
                $query = "SELECT nama_lengkap FROM user WHERE usr_email='$usr'";
                $this->exec_q($query, "nama_lengkap");
                $res = $this->result[0];
                $this->nama[1][$i] = $res;

            }
        }

        $this->exec_q($q_following, "profpic_id");
        $profpic_id = $this->result;

        //cari URLnya

        for ($i=0; $i<count($profpic_id); $i++){
            if ($profpic_id[$i] != null){
                $id = $profpic_id[$i];
                $query = "SELECT url FROM photo WHERE photo_id=$id";
                $this->exec_q($query, "url");
                $res = $this->result[0];
                $this->img_url[1][$i] = $res;
            }
        }
        
    }

    function getMemberGroup($g_id, $u){
        $q = "SELECT nama_lengkap FROM user,(SELECT usr_member FROM group_members WHERE group_id=$g_id
                                            AND usr_member!='$u') AS email
                WHERE email.usr_member=user.usr_email";

        $this->exec_q($q, "nama_lengkap");
        $temp = $this->result;
        $this->nama = $temp;
    }

}

?>
